<template>
  <el-card style="width: 1200px; margin: 0 auto" shadow="always">
    <el-form>
      <el-form-item label="属性名" prop="attrName">
        <el-input
          class="attr-input"
          v-model="attrName"
          placeholder="请输入属性名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <template>
          <el-button
            type="primary"
            @click="AddAttr()"
            :disabled="attrName ? false : true"
            >添加属性值</el-button
          >
        </template>
      </el-form-item>
      <el-form-item prop="valueName">
        <el-table :data="attrValueList">
          <el-table-column
            label="序号"
            type="index"
            width="80"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input
                :ref="`inputRef${$index}`"
                v-model="row.valueName"
                v-show="row.isEdit"
                @blur="SwitchToDiv(row, $index)"
              ></el-input>
              <div
                @click="SwitchToIpt(row, $index)"
                class="ml-10"
                v-show="!row.isEdit"
              >
                {{ row.valueName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteAttrValue($index)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="SaveAttrList">保存</el-button>
        <el-button @click="cancelAttr">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { SaveAttrInfoApi } from '@/api/AttrApi'

export default {
  name: 'AttrUpdate',
  data() {
    return {
      attrName: '',
      attrValueList: [],
      id: ''
    }
  },
  computed: {},
  methods: {
    cancelAttr() {
      this.$emit('SwitchAttrComponents', '1')
    },
    AddAttr() {
      this.attrValueList.push({
        valueName: '',
        isEdit: true
      })
      this.$nextTick(function () {
        this.$refs[`inputRef${this.attrValueList.length - 1}`].focus()
      })
    },
    SwitchToDiv(row, index) {
      if (!row.valueName) {
        this.attrValueList.splice(index, 1)
      }
      row.isEdit = false
    },
    SwitchToIpt(row, index) {
      row.isEdit = true
      this.$nextTick(() => {
        this.$refs[`inputRef${index}`].focus()
      })
    },
    async SaveAttrList() {
      let res = {}
      if (this.id) {
        //修改
        res = await SaveAttrInfoApi({
          attrName: this.attrName,
          attrValueList: this.attrValueList,
          categoryId: this.$store.state.sortStore.Category3Id,
          categoryLevel: 3,
          id: this.id
        })
      } else {
        //添加
        res = await SaveAttrInfoApi({
          attrName: this.attrName,
          attrValueList: this.attrValueList,
          categoryId: this.$store.state.sortStore.Category3Id,
          categoryLevel: 3
        })
      }

      if (res.code === 200) {
        this.attrName = ''
        this.attrValueList = []
        this.id = ''
        this.cancelAttr()
      }
    },
    deleteAttrValue(index) {
      console.log(index)
      this.attrValueList.splice(index, 1)
    }
  },
  props: {
    isShowAttrComponents: {
      type: String
    }
  },
  mounted() {
    this.attrName = this.$store.state.sortStore.attrName
    this.attrValueList = this.$store.state.sortStore.attrValueList
    this.id = this.$store.state.sortStore.attrid
  }
}
</script>

<style scoped>
.attr-input {
  width: 200px;
}
</style>
